package pl.mchaniewski.ryneczek.dao.impl;

import java.util.List;

import javax.persistence.NoResultException;
import javax.persistence.Query;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import pl.mchaniewski.ryneczek.dao.UserDao;
import pl.mchaniewski.ryneczek.pojo.User;

@Repository("userDao")
@Transactional(readOnly = true)
public class HibernateUserDao extends HibernateGenericDao<User, Integer>
        implements UserDao {

    @Override
    @Transactional(readOnly = false)
    public void remove(User user) {
        User tempUser = getEntityManager().getReference(User.class,
                user.getUserid());
        getEntityManager().remove(tempUser);
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<User> findByLogin(String username) throws NoResultException {
        String queryString = "from User where login=:username";
        Query query = getEntityManager().createQuery(queryString);
        query.setParameter("username", username);

        return query.getResultList();
    }

}
